home *** CD-ROM | disk | FTP | other *** search
- /* MicrosecondTimerDemo.c */
- /*
- * This is a totally useless demo program that scribbles
- * the time of day as quickly as it can, then it tries
- * to determine the cost of calling the time routines.
- * Click on the mouse whenever you get bored.
- */
- #include <stdio.h>
- #include <console.h>
- #include <limits.h>
- #include <math.h>
- #include "MicrosecondTimer.h"
- #define MILLION (1000000L)
- #define HIST_MAX 100
- #define HIST_SIZE (100L) /* 100 microseconds per bin */
- double histogram[HIST_MAX];
- double overflow;
-
- double DoubleDelta(
- MicrosecondEpoch *start,
- MicrosecondEpoch *finish
- );
-
- main(
- int argc,
- char **argv
- )
- {
- MicrosecondEpoch then;
- MicrosecondEpoch now;
- MicrosecondEpoch start;
- MicrosecondEpoch finish;
- long elapsed;
- long smallest;
- long biggest;
- double trials;
- double sum, sumSq;
- double mean, variance, sd;
- double seconds;
- double trialTime;
- register short i,top;
- Str255 message;
-
- /*
- * Your program may crash if you call Think C
- * stdio routines before initializing the
- * timer module.
- */
- InitializeMicrosecondTimer();
- argc = ccommand(&argv);
- printf("Hit the mouse to stop\n");
- GetEpoch(&then);
- while (Button() == FALSE) {
- SystemTask();
- GetEpoch(&now);
- EpochToString(&now, message);
- DeltaTime(&then, &now, &elapsed);
- printf(
- "%#s - %ld.%06ld\n",
- message,
- elapsed / MILLION,
- elapsed % MILLION
- );
- then = now;
- }
- while (Button())
- ;
- printf("Beginning timer resolution test.\n");
- printf("Hit the mouse to stop\n");
- smallest = LONG_MAX;
- biggest = 0;
- trials = 0;
- sum = sumSq = 0;
- i = 0;
- GetEpoch(&start);
- while (Button() == FALSE) {
- if ((++i % 1000) == 0)
- SystemTask();
- GetEpoch(&then);
- GetEpoch(&now);
- DeltaTime(&then, &now, &elapsed);
- if (elapsed < smallest)
- smallest = elapsed;
- if (elapsed > biggest)
- biggest = elapsed;
- sum += elapsed;
- sumSq += (elapsed * elapsed);
- trials++;
- i = elapsed / HIST_SIZE;
- if (i >= HIST_MAX)
- ++overflow;
- else {
- ++histogram[i];
- }
- }
- while (Button())
- ;
- GetEpoch(&finish);
- top = HIST_MAX;
- if (overflow == 0) {
- while (top > 0 && histogram[top - 1] == 0)
- --top;
- }
- printf("Each histogram bucket contains %ld µsec.\n",
- HIST_SIZE);
- for (i = 0; i < top; i++) {
- printf("%5ld: %.0f\n",
- i * HIST_SIZE, histogram[i]);
- }
- if (overflow > 0)
- printf("%.0f overflow\n");
- printf("Timer minimum = %ld.%06ld,",
- smallest / MILLION,
- smallest % MILLION
- );
- printf(" maximum = %ld.%06ld\n",
- biggest / MILLION,
- biggest % MILLION
- );
- printf("%.0f trials", trials);
- if (trials > 0) {
- mean = sum / trials;
- printf(": mean %.2f µsec.", mean);
- if (trials > 1.0) {
- /*
- * No, this is not the proper way to
- * calculate the variance.
- */
- variance = (sumSq * trials) - (sum * sum);
- variance /= (trials * trials);
- sd = sqrt(variance); /* Std. deviation */
- printf(", variance %0.2f,", variance);
- printf(" standard deviation %0.2f", sd);
- }
- }
- printf("\n");
- seconds = DoubleDelta(&start, &finish);
- printf("%0.3f seconds", seconds);
- if (seconds > 0.0)
- printf(", %0.2f trials/sec.", trials / seconds);
- if (trials > 0.0) {
- trialTime = (seconds * (double) MILLION) / trials;
- printf(", %.2f µsec./trial\n", trialTime);
- printf("%.2f µsec. mean test overhead",
- trialTime - mean
- );
- }
- printf("\n");
- }
-
- /*
- * Compute the time difference as a double-precision
- * number of seconds.
- */
- double
- DoubleDelta(
- MicrosecondEpoch *start,
- MicrosecondEpoch *finish
- )
- {
- double seconds;
- double microseconds;
- double result;
-
- seconds = finish->time - start->time;
- microseconds =
- finish->microsecond - start->microsecond;
- result = seconds
- + (microseconds / (double) MILLION);
- return (result);
- }
-
-